
import Pagination from '@/components/pagination';
import mixin from '@/service/mixin';
import editLoanDialog from './components/editLoanDialog'
import editChannelDialog from './components/editChannelDialog'

let that;
export default {
    name: 'change-manager',
    components: {
        Pagination,
        editLoanDialog,
        editChannelDialog
    },
    mixins: [mixin],
    data() {
        return {
            tableData: [],
            id: '',
            form: {
                code: '',
                isUse: '',
                companyCode: '',
                channelType: '',
                formTitle: '',
            },
            rules: {
            },
            dialogVisible: false,
            activeName: 'first',
            formLabelWidth: '120px',
            repayCompany: '',
            isVisibleRepayDialog: false,
            repayForm: {
                code: '',
                remark: []
            },
            companyData: [],
            loanForm: {
                appName: ''
            },
            repayChannelForm: {
                appName: '',
            },
            appList: [],
            repayChannelList: [], // 还款支付方式
            repaySearchForm: {
                appName: '',
            },
            onlineTable: [], // 还款渠道
            offlineTable: [], // 还款渠道
            instapayTable: [], // 还款渠道
            channelData:{},
        };
    },
    computed: {
        showChannelTab() {
            // 是否显示渠道
            return this.$store.getters.checkUserAuthority('finance.system-setting.product-setting.repayChannelTab');
        },
    },
    methods: {
        handleClick() {
        },
        // 编辑放款渠道
        editHandler(row, type) {
            const form = this.form;
            form.code = row.code || row.channelCode;
            form.isUse = row.isUse;
            const companyCode = row.companyCode || row.paymentCompanyCode;
            form.companyCode = companyCode ? companyCode : '';
            form.channelType = type;
            form.formTitle = 'loan' === type ? this.$tc('finance.changePayChannel') : row.name;
            this.getCompany('loan' === type ? 0 : 1, form.code, type);
        },
        // 编辑放款渠道提交
        editSubmit() {
            const form = this.form;
            if(form.isUse === 0 && !form.companyCode) {
                this.$message.error(this.$tc('verify.plsChoose'));
                return;
            }
            if ('loan' === form.channelType) {
                this.handleUpdateLoanChannel(form);
            } else if ('channel' === form.channelType) {
                this.handleUpdateChannel(form);
            }
        },
        // 处理修改放款渠道
        handleUpdateLoanChannel(form) {
            const params = {
                code: form.code,
                isUse: form.isUse,
                companyCode: form.companyCode,
                appName: this.loanForm.appName,
            };
            this.$api.finance.productSetting.updateLoanChannel(params).then((res) => {
                const {status, error} = res;
                if ('1' === status && error === '00000000') {
                    this.dialogVisible = false;
                    this.$message.success(this.$tc('common.successfulOperation'));
                    this.getLoanChannel();
                }
            });
        },
        // 处理修改还款渠道
        handleUpdateChannel(form) {
            const params = {
                channelCode: form.code,
                isUse: form.isUse,
                paymentCompanyCode: form.companyCode,
                appName: this.repayChannelForm.appName,
            };
            this.$api.finance.productSetting.modifyRepayChannelConfig(params).then((res) => {
                const {status, error} = res;
                if ('1' === status && error === '00000000') {
                    this.dialogVisible = false;
                    this.$message.success(this.$tc('common.successfulOperation'));
                    this.getRepayChannelConfigNew();
                }
            });
        },
        // 更改还款公司
        changeRepayChannel() {
            this.getCompany(1, '', 'repay');
        },
        // 更改还款公司提交
        changeSubmit() {
            const form = this.repayForm;
            const appName = this.repaySearchForm.appName;
            const params = {
                code: form.code,
                appName,
                // remark: form.remark.join(',')
                remark: '1,2,3'
            };
            this.$api.finance.productSetting.updateRepayCompnay(params).then((res) => {
                const {status, error} = res;
                if ('1' === status && error === '00000000') {
                    this.isVisibleRepayDialog = false;
                    this.$message.success(this.$tc('common.successfulOperation'));
                    this.getRepayCompany();
                    this.getRepayChannel();
                }
            });
        },
        // 查询放款渠道数据
        getLoanChannel() {
            const params = {
                // appName: this.loanForm.appName
            };
            this.$api.finance.productSetting.getLoanChannel(params).then((res) => {
                const {status, data, error} = res;
                if ('1' === status && error === '00000000' && data) {
                    data.forEach(item=>{
                        item.appNameList = item.appName.split(',')
                    })
                    this.tableData = data;
                    this.tableMaxHeight = window.innerHeight - 240;
                }
            });
        },
        // 获取支付渠道公司
        getCompany(payType,channelCode, type) {
            const params = {
                payType,
                channelCode
            };
            // if(payType === 0 && channelCode) params.channelCode = channelCode;
            this.$api.finance.productSetting.getCompanyCodeList(params).then((res) => {
                const {status, data, error} = res;
                if ('1' === status && error === '00000000' && data) {
                    this.companyData = data;
                    if ('repay' === type) {
                        this.isVisibleRepayDialog = true;
                    } else {
                        this.dialogVisible = true;
                    }
                    // if(payType === 0) this.dialogVisible = true;
                    // if(payType === 1) this.isVisibleRepayDialog = true;
                }
            });
        },
        // 获取当前还款公司信息
        getRepayCompany() {
            const appName = this.repaySearchForm.appName;
            this.$api.finance.productSetting.getRepayCompany().then((res) => {
                const {status, data, error} = res;
                if ('1' === status && error === '00000000' && data) {
                    this.repayCompany = data.name;
                    this.repayForm.code = data.code;
                    this.repayForm.remark = data.remark && data.remark.split(',');
                }
            });
        },
        // 根据domain获取APP列表
        getAppList() {
            const params = {
                appListDomain: this.$storage.get('user').domain,
            };
            this.$api.systemManage.getAppListdomain(params).then((res) => {
                const {status, data, error} = res;
                if ('1' === status && error === '00000000') {
                    this.appList = data || [];
                    this.loanForm.appName = this.appList[0].name;
                    this.repaySearchForm.appName = this.appList[0].name;
                    this.repayChannelForm.appName = this.appList[0].name;
                    this.getLoanChannel();
                    this.getRepayChannel();
                    this.getRepayCompany();
                    if (this.showChannelTab) {
                        this.getRepayChannelConfigNew();
                    }
                }
            });
        },
        // 获取还款渠道列表
        getRepayChannel() {
            const appName = this.repaySearchForm.appName;
            this.$api.finance.productSetting.getRepayChannelConfig({ appName }).then(res => {
                const { status, data } = res;
                if ('1' === status && data) {
                    this.repayChannelList = data;
                }
            });
        },
        // 更改还款方式启用状态
        handleStatusChange(row) {
            const appName = this.repaySearchForm.appName;
            const params = {
                id: row.id,
                isUse: row.isUse,
                appName,
            };
            this.$api.finance.productSetting.updateRepayChannelStatus(params).then(res => {
                const { status } = res;
                if ('1' === status) {
                    this.getRepayChannel();
                    this.$message.success(this.$tc('common.saveSuccess'));
                }
            });
        },
        // 更改还款APP选择
        handleRepayAppChange() {
            this.getRepayChannel();
            this.getRepayCompany();
        },
        // 更改还款支付公司
        handleRepayCompanyChange() {
            // val
            // if (val === 4) {
            //     this.repayForm.remark = [];
            // } else {
            //     this.repayForm.remark = ['1','2','3'];
            // }
        },
        // 获取还款渠道
        getRepayChannelConfigNew() {
            const params = {
                // appName: this.repayChannelForm.appName
            };
            this.$api.finance.productSetting.getRepayChannelConfigNew(params).then((res) => {
                const {status, data, error} = res;
                if ('1' === status && error === '00000000' && data) {
                    const { online, offline, instapay } = data;
                    this.onlineTable = online;
                    this.offlineTable = offline;
                    this.instapayTable = instapay;
                    this.channelData = data
                }
            });
        },
    },
    filters: {
        switchStatus(type) {
            switch (type + '') {
                case '0':
                    return that.$t('collection.activated');// 已启用
                case '1':
                    return that.$t('common.notEnable');// 未启用
                default:
                    return type;
            }
        }
    },
    created() {
        that = this;
        this.getAppList();
        // this.getLoanChannel();
        // this.getRepayCompany();
        // this.getRepayChannel();
    }
};
